package com.example.app.viewmodel

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.example.app.model.entity.Category
import com.example.app.model.entity.DataType
import com.example.app.model.entity.SwiperEntity
import com.example.app.model.service.HomeService

class MainViewModel : ViewModel() {

    private val homeService = HomeService.instance()

    //分类数据是否加载成功
    var categoryLoaded by mutableStateOf(false)
        private set

    //分类数据
    var categories by mutableStateOf(
        listOf(
            Category("", "1"),
            Category("", "1"),
            Category("", "1"),
            Category("", "1"),
        )
    )
        private set

    suspend fun categoryData() {
        val categoryRes = homeService.category()
        if (categoryRes.code == 200) {
            categories = categoryRes.data
            categoryLoaded = true
        } else {
//            不成功的情况下读取message
            val message = categoryRes.message
        }
    }

    //当前分类下标
    var categoryIndex by mutableIntStateOf(0)
        private set

    fun updateCategoryIndex(index: Int) {
        categoryIndex = index
    }

    /**
     * 类型数据
     */
    val types by mutableStateOf(
        listOf(
            DataType("相关资讯", Icons.Filled.Menu),
            DataType("视频课程", Icons.Filled.PlayArrow),
        )
    )
    var typeIndex by mutableIntStateOf(0)
        private set

    fun updateTypeIndex(index: Int) {
        typeIndex = index
    }

    /**
     * 轮播图数据
     */
    var swiperData by mutableStateOf(
        listOf(
            SwiperEntity("https://images.unsplash.com/photo-1518791841217-8f162f1e1131?spm=5176.28103460.0.0.6d0e5d27IE9WAc&ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"),
            SwiperEntity("https://picsum.photos/300/200"),

            )
    )
        private set
    suspend fun swiperData() {
        val swiperRes = homeService.banner()
        if (swiperRes.code == 200 && swiperRes.data!=null) {
            swiperData = swiperRes.data
        } else {
//            不成功的情况下读取message
            val message = swiperRes.message
        }
    }

    //通知数据
    val notifications = listOf(
        "美国众议院通过了一项重要的气候法案",
        "苹果公司发布了全新的iPhone 15系列手机",
        "第三季度国内生产总值（GDP）同比增长5.2%",
        "上海市宣布将从下月起实施垃圾分类新规",
        "知名演员李华凭借在电影《追风筝》中的出色表现"
    )
}
